﻿using System;
using System.Diagnostics;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYCodeBase.WinForm;
using HYElectricDim;
using YRevitJig;
using YJKRGeometry;
using HYView;

namespace ElectricDim
{
	[Transaction(TransactionMode.Manual)]
	public class CmdLampLabel : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			WindowHandle windowHandle = null;
			if (windowHandle == null)
			{
				windowHandle = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			DlgLampLabel instance = DlgLampLabel.GetInstance(commandData.Application, windowHandle);
			instance.Show(windowHandle);
			double num = (double)document.ActiveView.Scale;
			new SubTransaction(activeUIDocument.Document);
			for (;;)
			{
				Transaction transaction = new Transaction(document, "LayLabel");
				try
				{
					transaction.Start();
					Reference reference = activeUIDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new LampFilter());
					Element element = document.GetElement(reference);
					if (element != null)
					{
						XYZ xyz = new XYZ(reference.GlobalPoint.X, reference.GlobalPoint.Y, document.ActiveView.SketchPlane.GetPlane().Origin.Z);
						try
						{
							ElementIdDate elementIdDate = Assist.ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element, this.guid);
							if (elementIdDate.GetId().IntegerValue != 0)
							{
								document.Delete(elementIdDate.GetId());
							}
						}
						catch
						{
						}
						Autodesk.Revit.DB.ElementType elementType = instance.GetElementType();
						IndependentTag independentTag = null;
						if (elementType != null)
						{
							ElectricDimJig electricDimJig = new ElectricDimJig(activeUIDocument);
							if (instance.GetDimStyle() == LeaderStyle.Orthogonal)
							{
								electricDimJig.ResetPoint = XUiJigBase.EResetPoint.eByOrtho;
							}
							electricDimJig.RectWidth = AssistFunc.mmToFeet(36.0) * num;
							electricDimJig.RectHeight = AssistFunc.mmToFeet(10.0) * num;
							XYZ xyz2 = electricDimJig.PickDimPoint(xyz, ElectricDimJig.EDimType.eSideBorder, "请拾取标注点:");
							electricDimJig.Dispose();
							if (xyz2 == null || xyz2.DistanceTo(xyz) < 0.005)
							{
								transaction.RollBack();
								break;
							}
							independentTag = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, xyz2);
							independentTag.ChangeTypeId(elementType.Id);
							LeaderStyle dimStyle = instance.GetDimStyle();
							if (dimStyle == LeaderStyle.HasLeader || dimStyle == LeaderStyle.Orthogonal)
							{
                                independentTag.LeaderEndCondition = (LeaderEndCondition)1;
								independentTag.HasLeader = true;
								independentTag.LeaderEnd = reference.GlobalPoint;
								independentTag.LeaderElbow = xyz2;
								if (Geometry.LessThan(xyz2.X, xyz.X))
								{
									double num2 = AssistFunc.mmToFeet(25.0) * num;
									independentTag.TagHeadPosition = xyz2 - new XYZ(num2, 0.0, 0.0);
								}
								else
								{
									double num3 = AssistFunc.mmToFeet(20.0) * num;
									independentTag.TagHeadPosition = xyz2 + new XYZ(num3, 0.0, 0.0);
								}
							}
						}
						transaction.Commit();
						ElementIdDate obj = new ElementIdDate(independentTag.Id);
						Assist.ExtensibleStorageManager.WriteToElement(element, this.guid, obj);
						continue;
					}
					transaction.RollBack();
				}
				catch (Exception)
				{
					instance.Close();
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				break;
			}
			instance.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Family FindFamily(Autodesk.Revit.DB.Document doc, string familyName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Family));
			if (filteredElementCollector != null)
			{
				foreach (Element element in filteredElementCollector.ToElements())
				{
					if (((Family)element).Name == familyName)
					{
						return (Family)element;
					}
				}
			}
			return null;
		}

		private Guid guid = new Guid("356cda5a-e6c5-4c2f-a9ef-b1223336b8c8");
	}
}
